home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / dbms_mag / 9108 / roti2.aug < prev    next >
Text File  |  1991-06-27  |  2KB  |  70 lines

  1.  
  2. Listing 2
  3.  
  4. DECLARE
  5.   CURSOR trans IS
  6.   SELECT transaction_number, account_id,
  7.          transaction_type, amount
  8.   FROM   transactions
  9.   ORDER  BY transaction_number
  10.   FOR    UPDATE OF posted;
  11.   used   NUMBER(6);
  12.   limit  NUMBER(6);
  13.   purchase_too_big EXCEPTION;
  14.   payment_too_big EXCEPTION;
  15.   invalid_transaction_type EXCEPTION;
  16. BEGIN
  17.   FOR t IN trans LOOP
  18.     BEGIN -- start an inner block
  19.       -- get credit used/limit and validate account
  20.       SELECT credit_used, credit_limit
  21.       INTO   used, limit
  22.       FROM   accounts
  23.       WHERE  account_id = t.account_id;
  24.       -- process transaction based on type:
  25.       -- P = purchase, C = credit (payment)
  26.       IF t.transaction_type = 'P' THEN
  27.         IF t.amount > limit - used THEN
  28.           RAISE purchase_too_big;
  29.         END IF;
  30.         UPDATE accounts
  31.         SET    credit_used = credit_used + t.amount
  32.         WHERE  account_id = t.account_id;
  33.       ELSIF t.transaction_type = 'C' THEN
  34.         IF t.amount > used THEN
  35.           RAISE payment_too_big;
  36.         END IF;
  37.         UPDATE accounts
  38.         SET    credit_used = credit_used - t.amount
  39.         WHERE  account_id = t.account_id;
  40.       ELSE
  41.         RAISE invalid_transaction_type;
  42.       END IF;
  43.       -- mark transaction as posted if update succeeded
  44.       UPDATE transactions
  45.       SET    posted = 'Y'
  46.       WHERE  CURRENT OF trans;
  47.     -- handle error conditions
  48.     EXCEPTION
  49.       WHEN NO_DATA_FOUND THEN -- account_id not found
  50.         INSERT INTO errors
  51.         VALUES (t.transaction_number,
  52.                 'Invalid account: ' || t.account_id);
  53.       WHEN purchase_too_big THEN
  54.         INSERT INTO errors
  55.         VALUES (t.transaction_number,
  56.                 'Purchase too big: ' || TO_CHAR(t.amount));
  57.       WHEN payment_too_big THEN
  58.         INSERT INTO errors
  59.         VALUES (t.transaction_number,
  60.                 'Payment too big: ' || TO_CHAR(t.amount));
  61.       WHEN invalid_transaction_type THEN
  62.         INSERT INTO errors
  63.         VALUES (t.transaction_number,
  64.                 'Invalid type: ' || t.transaction_type);
  65.     END;
  66.   END LOOP;
  67.   COMMIT;
  68. END;
  69. /
  70.